package org.web.standardplatform.client.domain.attribute;

import com.google.gson.Gson;
import lombok.Getter;
import lombok.Setter;
import org.web.base.helper.StringHelper;


import java.util.List;

@Setter
@Getter
public class JavaAttributeDTO {

    private String columnName;
    private String attributeName;
    private String attributeType;
    @Setter
    private List<String> dtoRuleList;
    private List<String> domainRuleList;

    private String supportEqual; //不设置， 默认不支持
    private String supportIn; // 不设置， 默认不支持
    private String supportCompare; // 不设置， 默认不支持
    private String supportFuzzy; // 不设置， 默认不支持

    private String javaAttributeExtendJson; // 扩展属性值，格式是JSON字符串

    private JavaAttributeExtendDTO javaAttributeExtendDTO;

    public JavaAttributeExtendDTO getJavaAttributeExtendDTO() {
        if (javaAttributeExtendDTO == null && javaAttributeExtendJson != null) {
            Gson gson = new Gson();
            return gson.fromJson(javaAttributeExtendJson, JavaAttributeExtendDTO.class);
        }
        return javaAttributeExtendDTO;
    }

    public List<String> getDtoRuleList() {
        if (dtoRuleList == null && dtoRules != null) {
            String[] lines = dtoRules.split("\\n");
            dtoRuleList = java.util.Arrays.asList(lines);
        }
        return dtoRuleList;
    }

    public String getDtoRules() {
        if (dtoRules == null && dtoRuleList != null) {
            StringBuilder sb = new StringBuilder();
            for (String rule : dtoRuleList) {
                sb.append(rule).append("\n");
            }
            dtoRules = sb.toString();
        }
        return dtoRules;
    }

    public void setDtoRules(String rules) {
        this.dtoRules = rules;
    }

    private String dtoRules;

    private String domainRules;

    public List<String> getDomainRuleList() {
        if (domainRuleList == null && domainRules != null) {
            String[] lines = domainRules.split("\\n");
            domainRuleList = java.util.Arrays.asList(lines);
        }
        return domainRuleList;
    }

    public void setDomainRuleList(List<String> ruleList) {
        this.domainRuleList = ruleList;
    }

    public String getDomainRules() {
        if (domainRules == null && domainRuleList != null) {
            StringBuilder sb = new StringBuilder();
            for (String rule : domainRuleList) {
                sb.append(rule).append("\n");
            }
            domainRules = sb.toString();
        }
        return domainRules;
    }

    public void setDomainRules(String rules) {
        this.domainRules = rules;
    }


    public String getInitUpperAttributeName() {
        return StringHelper.initUpper(attributeName);
    }

    public String getAttributeGetMethod() {
        return getAttributeGetMethodName() + "()";
    }

    public String getAttributeGetMethodName() {
        return "get" + StringHelper.initUpper(attributeName);
    }

    public String getAttributeSetMethod() {
        return "set" + StringHelper.initUpper(attributeName);
    }

    public String getAttributeSetMethodName() {
        return getAttributeSetMethod();
    }

    public String getFuzzyQueryAttributeName() {
        return "fuzzy" + StringHelper.initUpper(attributeName);
    }

    public String getInQueryAttributeName() {
        return StringHelper.initLower(attributeName) + "Set";
    }

}
